package cn.getech.data.development.service.impl;

import cn.getech.data.development.constant.RangerPolicyType;
import cn.getech.data.development.entity.UserRangerPolicyRel;
import cn.getech.data.development.mapper.UserRangerPolicyRelMapper;
import cn.getech.data.development.service.UserRangerPolicyRelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRangerPolicyRelServiceImpl extends ServiceImpl<UserRangerPolicyRelMapper, UserRangerPolicyRel> implements UserRangerPolicyRelService {

    @Autowired
    private UserRangerPolicyRelMapper userRangerPolicyRelMapper;

    @Override
    public List<UserRangerPolicyRel> selectAllListByProcIdAndList(Integer userId, List<Integer> tableIds) {
        return userRangerPolicyRelMapper.selectAllListByProcIdAndList(userId, tableIds);
    }

    @Override
    public List<UserRangerPolicyRel> selectAllListByUserIdsAndTableId(Integer tableId, List<Long> allUserIds) {
        return userRangerPolicyRelMapper.selectAllListByUserIdsAndTableId(tableId, allUserIds);
    }

    @Override
    public void deleteByTableId(Integer tableId) {
        userRangerPolicyRelMapper.delete(new QueryWrapper<UserRangerPolicyRel>().eq("table_id",tableId));
    }

    @Override
    public List<UserRangerPolicyRel> selectAllListByUserId(Long userId) {
        return userRangerPolicyRelMapper.selectList(new QueryWrapper<UserRangerPolicyRel>().eq("user_id",userId));
    }

    @Override
    public void deleteByUserId(Long userId) {
        userRangerPolicyRelMapper.delete(new QueryWrapper<UserRangerPolicyRel>().eq("user_id",userId));
    }

    @Override
    public UserRangerPolicyRel limitOneByTableIdAndRowLevel(Integer tableId) {
        return userRangerPolicyRelMapper.selectOne(new QueryWrapper<UserRangerPolicyRel>().eq("policy_type", RangerPolicyType.ROW_LEVEL_FILTER.getId()).eq("table_id",tableId));
    }

    @Override
    public void deleteById(Integer id) {
        userRangerPolicyRelMapper.deleteById(id);
    }

    @Override
    public List<UserRangerPolicyRel> selectMaskingRulesData(Integer tableFieldId) {
        return userRangerPolicyRelMapper.selectList(new QueryWrapper<UserRangerPolicyRel>().eq("policy_type",RangerPolicyType.MASKING.getId()).eq("table_field_id",tableFieldId));
    }

    @Override
    public List<UserRangerPolicyRel> selectMaskingRulesDataByTableId(Integer tableId) {
        return userRangerPolicyRelMapper.selectList(new QueryWrapper<UserRangerPolicyRel>().eq("policy_type",RangerPolicyType.MASKING.getId()).eq("table_id",tableId));
    }


}
